<template>
	<view class="page">
		<view class="page-tabs">
			<view class="fo">
				<scroll-view
					scroll-x="true"
					class="scroll">
					<view class="tabs">
						<view
							v-for="(item,i) in tabs"
							:key="item.value"
							@click="changeTabs(item)"
							:class="{
								'item': true,
								'is-active': orderType == item.value
							}">
							<view class="cell">
								<view class="text">
									<text>{{ item.label }}</text>
									<text vif="item.total">（{{ item.total }}）</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<view
					class="filter"
					@click="open('timePopup')">
					<image
						class=""
						src="https://img.2024csnl.com/static/2.x/a101.png">
					</image>
					<text>{{ '筛选' || time }}</text>
				</view>
			</view>
		</view>
		
		<view class="page-list">
			<view class="list">
				<view
					v-for="(item, index) in tableData"
					:key="index"
					class="item">
					<view class="cell">
						<view class="tbody">
							<view class="cover">
								<image :src="item.pic" mode=""></image>
							</view>
							<view class="exp">
								<view class="title">
									<text>{{ item.name }}</text>
								</view>
								<view class="price">
									<text>￥</text>
									<text>{{ item.sell_price }}</text>
								</view>
								<view class="desc">
									<view>
										<text>总销量：</text>
										<text>{{ item.sales }}</text>
									</view>
									<view>
										<text>库存：</text>
										<text>{{ item.stock }}</text>
									</view>
								</view>
							</view>
						</view>
						<view class="tfoot">
							<view class="btns">
								<!-- <view class="btn">
									<text>按钮文案</text>
								</view> -->
								<view
									v-if="item.status == 1"
									class="btn is-down"
									@click="handleDown(item)">
									<text>下架</text>
								</view>
								<view
									v-if="item.status == 0"
									class="btn is-up"
									@click="handleUp(item)">
									<text>上架</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import { $get, $post } from "@/api/index.js"
	
	export default {
		data() {
			return {
				option: {},
				orderType: '1',
				tabs: [
					{ label: '出售中', value: '1', total: 0 },
					{ label: '已下架', value: '0', total: 0 }
				],
				tableData: []
			}
		},
		onLoad(option) {
			this.option = option
			this.getTableData()
		},
		onReachBottom() {
			this.pagenum += 1
			this.getTableData(true)
		},
		methods: {
			changeTabs ({ value }) {
				this.orderType = value;
				this.getTableData();
			},
			async getTableData (isadd) {
				const { bid } = this.option;
				const res = await $get('s=/ApiBusiness/businessshopproduct', {
					bid,
					status: this.orderType,
					pagenum: this.pagenum
				})
				const { data, status, msg } = res.data
				if (status == 1) {
					isadd ? this.tableData.push(...data.data) : this.tableData = data.data
					if (this.orderType == 0) {
						this.tabs[1].total = data.total
					} else if (this.orderType == 1) {
						this.tabs[0].total = data.total
					}
				} else {
					this.showToast(msg)
				}
			},
			// 下架
			handleDown ({ id }) {
				uni.showModal({
					content: '确定要将该商品下架吗？',
					success: async (res) => {
						if (res.confirm) {
							const res = await $post('s=/ApiBusiness/bshopcz', {
								id,
								type: 2 // 1 上架, 2 下架, 3 删除
							});
							uni.showToast({
								title: '下架成功'
							});
							this.tabs[1].total += 1
							this.getTableData();
						}
					}
				});
			},
			// 上架
			handleUp ({ id }) {
				uni.showModal({
					content: '确定要将该商品上架吗？',
					success: async (res) => {
						if (res.confirm) {
							const res = await $post('s=/ApiBusiness/bshopcz', {
								id,
								type: 1 // 1 上架, 2 下架, 3 删除
							});
							uni.showToast({
								title: '上架成功'
							});
							this.tabs[0].total += 1
							this.getTableData();
						}
					}
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	.page {
		position: relative;
		min-height: 100vh;
		background: #f8f8f8;
	}
	
	.page-tabs {
		position: sticky;
		top: 0;
		z-index: 100;
		background: #fff;
		border-radius: 0 0 20rpx 20rpx;
		.fo {
			position: relative;
			display: flex;
			align-items: center;
			.scroll {
				flex: 1;
				height: 100rpx;
				.tabs {
					display: flex;
					align-items: center;
					.item {
						flex: 1;
						&.is-active {
							.cell {
								&:before {
									content: '';
									position: absolute;
									left: 50%;
									top: 60rpx;
									width: 120rpx;
									margin-left: -60rpx;
									height: 14rpx;
									border-radius: 20rpx;
									background: -webkit-linear-gradient(left, #FDD69F, #fff);
								}
								.text {
									font-weight: bold;
									color: #000;
								}
							}
						}
					}
					.cell {
						position: relative;
						.text {
							position: relative;
							z-index: 2;
							display: block;
							height: 100rpx;
							line-height: 100rpx;
							text-align: center;
							font-size: 30rpx;
							color: #999;
							text:nth-child(2) {
								font-size: 28rpx;
								color: #999;
								font-weight: normal !important;
								
							}
						}
					}
				}
			}
			.filter {
				width: 160rpx;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				text {
					font-size: 30rpx;
					margin-left: 10rpx;
				}
				image {
					display: block;
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
	}
	
	.page-list {
		padding: 0 0 20rpx;
		.list {}
		.item {}
		.cell {
			position: relative;
			padding: 20rpx;
			background: #fff;
			border-radius: 20rpx;
			margin: 20rpx;
		}
		.tbody {
			display: flex;
			align-items: center;
		}
		.tfoot {
			display: flex;
			align-items: center;
			justify-content: flex-end;
		}
		.cover {
			image {
				display: block;
				width: 180rpx;
				height: 180rpx;
				background: #eee;
				border-radius: 10rpx;
			}
		}
		.exp {
			position: relative;
			margin: 0 0 0 20rpx;
		}
		.title {
			line-height: 40rpx;
			text-align: justify;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			text {
				font-size: 30rpx;
				font-weight: bold;
			}
		}
		.price {
			line-height: 40rpx;
			font-size: 28rpx;
			font-weight: bold;
			color: #E86A33;
			margin: 10rpx 0;
		}
		.desc {
			line-height: 40rpx;
			color: #999;
			display: flex;
			align-items: center;
			view {
				margin-right: 20rpx;
			}
		}
		.btns {
			display: flex;
			align-items: center;
			.btn {
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 60rpx;
				padding: 0 30rpx;
				background: #fff;
				border: 1px solid #aaa;
				margin: 20rpx 0 0 20rpx;
				color: #666;
				&.is-down {
					background: #E86A33;
					border-color: #E86A33;
					color: #fff;
				}
				&.is-up {
					background: #41644A;
					border-color: #41644A;
					color: #fff;
				}
			}
		}
	}
</style>
